/**
 * @description 验收信息控制层
 * @date 2020-08-21 14:04
 * @auth LI
 */

const {
  createAcceptanceInfo,
  getAcceptanceInfo,
  getAccountInfo,
  sendSMSCodeByPhone,
  updateAcceptanceInfoById,
} = require('../services/acceptanceInfo')
const { getHouseOwner, delHouseOwner } = require('../cache/phoneCode')

const { ReturnResult } = require('../model/ResultModel')

/**
 * 添加验收信息
 * @param {number} workerAccountId 对应工人账号 id
 * @param {number} houseInfoId 对应房源信息 id
 * @param {number} houseOwnerId 对应业主信息 id
 * @param {number} typeLevel 对应账号类型
 * @param {string} acceptancePhoto 验收图片
 * @param {string} acceptanceTime 验收时间
 * @param {string} remark 备注/要求
 */
async function addAcceptanceInfo({ workerAccountId, houseInfoId, houseOwnerId, typeLevel, acceptancePhoto, acceptanceTime, remark }) {
  try {
    // 查看是否创建过相同类型的验收信息
    const acceptanceInfo = await getAcceptanceInfo({ workerAccountId, houseInfoId, typeLevel })
    const accountInfo = await getAccountInfo(houseOwnerId)
    if (acceptanceInfo) {
      // 账号已存在
      return new ReturnResult(0, null, false, "已提交过此类型的信息")
    }
    if (!accountInfo) {
      // 账号已存在
      return new ReturnResult(0, null, false, "没有此房源信息")
    }
    
    // 发送验收码给业主手机号
    const phone = accountInfo.account
    await sendSMSCodeByPhone({ phone, workerAccountId, houseInfoId, typeLevel })
    // 接下来交给服务层，
    // 此处可以返回新增等数据库之后的信息 const user = 信息，最后返回这个 user 就是新增数据库的信息
    await createAcceptanceInfo({
      workerAccountId,
      houseInfoId,
      typeLevel,
      acceptancePhoto,
      acceptanceTime,
      remark
    })
    return new ReturnResult(0, null, true, "添加成功")
  } catch (e) {
    console.error(e.message, e.stack)
    return new ReturnResult(0, null, false, e.message)
  }
}

/**
 * 确认验收信息
 * @param {number} workerAccountId 对应工人账号 id
 * @param {number} houseInfoId 对应房源信息 id
 * @param {number} houseOwnerId 对应业主信息 id
 * @param {number} typeLevel 对应账号类型
 * @param {number} code 验收码
 */
async function acceptanceConfirm({ workerAccountId, houseInfoId, houseOwnerId, typeLevel, code }) {
  try {
    // 查看是否创建过相同类型的验收信息
    const acceptanceInfo = await getAcceptanceInfo({ workerAccountId, houseInfoId, typeLevel })
    const accountInfo = await getAccountInfo(houseOwnerId)
    if (!acceptanceInfo) {
      // 账号已存在
      return new ReturnResult(0, null, false, "没有提交验收信息")
    }
    if (!accountInfo) {
      // 账号已存在
      return new ReturnResult(0, null, false, "没有此房源信息")
    }
    // 通过业主手机号查询验收码
    const phone = accountInfo.account
    const ownerCode = await getHouseOwner(phone, workerAccountId, houseInfoId, typeLevel)
    if (Number(ownerCode) !== code) {
      return new ReturnResult(0, null, false, "验收码错误或未提交验收信息")
    }
    await delHouseOwner(phone, workerAccountId, houseInfoId, typeLevel)
    const acceptanceTime = new Date()
    await updateAcceptanceInfoById({ workerAccountId, houseInfoId, typeLevel, acceptanceTime })
    
    return new ReturnResult(0, null, true, "验收成功")
  } catch (e) {
    console.error(e.message, e.stack)
    return new ReturnResult(0, null, false, e.message)
  }
}

/**
 * 获取验收信息
 * @param {number} workerAccountId 对应工人账号 id
 * @param {number} houseInfoId 对应房源信息 id
 * @param {number} typeLevel 对应账号类型
 */
async function getAcceptanceList({ workerAccountId, houseInfoId, typeLevel }) {
  try {
    const acceptanceInfo = await getAcceptanceInfo({ workerAccountId, houseInfoId, typeLevel })
    
    return new ReturnResult(0, acceptanceInfo, true, "获取成功")
  } catch (e) {
    console.error(e.message, e.stack)
    return new ReturnResult(0, null, false, e.message)
  }
}

/**
 * 更新验收信息
 * @param {number} id 验收信息 id
 * @param {number} typeLevel 对应账号类型id
 * @param {string} reserve 验收评价
 */
async function updateAcceptanceInfo({ id, typeLevel, reserve }) {
  try {
    const acceptanceInfo = await updateAcceptanceInfoById({ id, typeLevel, reserve })
    
    return new ReturnResult(0, acceptanceInfo, true, "成功")
  } catch (e) {
    console.error(e.message, e.stack)
    return new ReturnResult(0, null, false, e.message)
  }
}

module.exports = {
  addAcceptanceInfo,
  acceptanceConfirm,
  getAcceptanceList,
  updateAcceptanceInfo
}
